﻿using AutoMapper.Internal.Mappers;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ThridGroup.ERP.DTO;
using ThridGroup.ERP.WarehouseManagement;

namespace ThridGroup.ERP.Web.Controllers.WarehouseManagement
{
    [Route("[controller]")]
    public class SaleController : Controller
    {
        private readonly SaleService _saleService;
        public SaleController(SaleService saleService)
        {
            _saleService = saleService;
        }
        
        // API方法
        [HttpPost("AddSale")]
        public async Task<int> AddSale([FromForm] SaleDTO sale)
        {
            return await _saleService.AddSale(sale);
        }
        
        [HttpDelete("DeleteSale")]
        public async Task<int> DeleteSale(int saleId)
        {
            return await _saleService.DeleteSale(saleId);
        }
        
        [HttpGet("FanSale")]
        public async Task<SaleDTO> FanSale(int saleId)
        {
            return await _saleService.FanSale(saleId);
        }
        
        [HttpGet("GetSale")]
        public async Task<JsonResult> GetSale(int pageIndex = 1, int pageSize = 10)
        {
            var list = await _saleService.GetSale();
            var result = list.OrderBy(c => c.Id).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return Json(new
            {
                code = 0,
                msg = "",
                count = list.Count,
                data = result,
            });
        }
        
        [HttpPost("UpdateSale")]
        public async Task<int> UpdateSale([FromForm] SaleDTO sale)
        {
            return await _saleService.UpdateSale(sale);
        }

        // 视图方法
        [HttpGet("SaleList")]
        public IActionResult SaleList()
        {
            return View();
        }

        [HttpGet("Add")]
        public IActionResult AddSaleView()
        {
            return View("AddSale");
        }

        [HttpGet("Edit")]
        public IActionResult EditSaleView()
        {
            return View("EditSale");
        }
    }
}
